package com.adobe.apprise.commands
{
	import com.adobe.apprise.database.Database;
	import com.adobe.apprise.database.DatabaseEvent;
	import com.adobe.apprise.database.DatabaseResponder;
	import com.adobe.apprise.events.PopulateViewControlEvent;
	import com.adobe.apprise.events.RefreshAllEvent;
	import com.adobe.apprise.model.ModelLocator;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.resources.ResourceManager;	
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.filesystem.*;
	import flash.system.Capabilities;
	
	import mx.collections.ArrayCollection;
	
	public class InitCommand implements ICommand
	{
		public function execute(e:CairngormEvent):void
		{
			var ml:ModelLocator = ModelLocator.getInstance();

			// Set up the database
			var sqlFile:File = File.applicationDirectory.resolvePath("sql.xml");
			var sqlFileStream:FileStream = new FileStream();
			sqlFileStream.open(sqlFile, FileMode.READ);
			var sql:XML = new XML(sqlFileStream.readUTFBytes(sqlFileStream.bytesAvailable));
			sqlFileStream.close();
			var db:Database = new Database(sql);
			var responder:DatabaseResponder = new DatabaseResponder();
			responder.addEventListener(DatabaseEvent.RESULT_EVENT,
				function(e:DatabaseEvent):void
				{
					ml.db = db;
					createFeedsTable();
				});
			db.initialize(responder);
		}
		
		private function createFeedsTable():void
		{
			var responder:DatabaseResponder = new DatabaseResponder();
			responder.addEventListener(DatabaseEvent.RESULT_EVENT, createPostsTable);
			ModelLocator.getInstance().db.createFeedsTable(responder);			
		}

		private function createPostsTable(e:Event):void
		{
			var responder:DatabaseResponder = new DatabaseResponder();
			responder.addEventListener(DatabaseEvent.RESULT_EVENT, createAuthorsTable);
			ModelLocator.getInstance().db.createPostsTable(responder);			
		}

		private function createAuthorsTable(e:Event):void
		{
			var responder:DatabaseResponder = new DatabaseResponder();
			responder.addEventListener(DatabaseEvent.RESULT_EVENT, createTopicsTable);
			ModelLocator.getInstance().db.createAuthorsTable(responder);			
		}

		private function createTopicsTable(e:Event):void
		{
			var responder:DatabaseResponder = new DatabaseResponder();
			responder.addEventListener(DatabaseEvent.RESULT_EVENT, start);
			ModelLocator.getInstance().db.createTopicsTable(responder);			
		}
		
		private function start(e:Event):void
		{
			var ml:ModelLocator = ModelLocator.getInstance();
			ml.db.initSynchronousConnection();
			// Initialize data.
			ml.statusMessage = ResourceManager.getInstance().getString('resources', 'INIT_WELCOME_MSG');
			ml.posts = new ArrayCollection();
			ml.totals = new ArrayCollection();
			ml.totals.addItemAt({"viewName":ResourceManager.getInstance().getString('resources', 'VIEWCONTROL_ALL_UNREAD'), "count":0}, 0);
			ml.totals.addItemAt({"viewName":ResourceManager.getInstance().getString('resources', 'VIEWCONTROL_CHECKED'), "count":0}, 1);
			
			// Populate the ViewControl.
			new PopulateViewControlEvent().dispatch();
			
			// Show the application window.
			NativeApplication.nativeApplication.openedWindows[0].visible = true;
			
			// Do the initial aggregation.
			new RefreshAllEvent().dispatch();
		}
	}
}
